package inheritance;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import util.HibernateUtil;

public class TestInheritance {
	//保存子类--Book和DVD
	@Test
	public void testSave()
	{
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();

		Book book = new Book();
		book.setName("book2");
		book.setPageCount(20000);
		
		DVD dvd = new DVD();
		dvd.setName("dvd2");
		dvd.setRegionCode("02");
		
		session.save(book);
		session.save(dvd);
		
		tx.commit();
		
	}
	
	//查询Book
	@Test
	public void testRead()
	{
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();

		Query query = session.createQuery("from Book");
		List<Book> books = query.list();
		for(Book book : books)
		{
			System.out.println(book.getId()+","+book.getName()+","+book.getPageCount());
		}
		
		tx.commit();
		
	}
	
	//查询所有商品
	@Test
	public void testRead2()
	{
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();

		Query query = session.createQuery("from Product");
		List<Product> products = query.list();
		for(Product product : products)
		{
			System.out.println(product.getClass().getSimpleName());
			System.out.println(product.getId()+","+product.getName());
		}
		
		tx.commit();
		
	}
}
